package com.cn.transfer.jobhandler.purchaserequisition;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvPuPurchaseRequisition;
import com.cn.transfer.common.entity.VvPuPurchaseRequisitionB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.utils.exception.BizException;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvPuPurchaseRequisitionBMapper;
import com.cn.transfer.mapper.VvPuPurchaseRequisitionMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 向伟典
 * @date: 2023/9/21 10:59
 * @Description: 定时删除弃审的数据
 */
@Component
public class PurchaseRequisitionUnAuditXxlJob {
    @Autowired
    private VvPuPurchaseRequisitionMapper vvPuPurchaseRequisitionMapper;
    @Autowired
    private VvPuPurchaseRequisitionBMapper vvPuPurchaseRequisitionBMapper;

    @Transactional
    @XxlJob("RequisitionUnAudit")
    public void requisitionUnAudit(){
        List<String> code = vvPuPurchaseRequisitionMapper.byChangedateListCode();
        List<String> delete = vvPuPurchaseRequisitionMapper.byChangedateListCodeDelete();
        for (String s : delete) {
            Integer integer = vvPuPurchaseRequisitionMapper.inquireIsExistPu(s);
            if (integer==0){
                code.add(s);
            }
        }
        for (String s : code) {
            VvPuPurchaseRequisition vvPuPurchaseRequisition1 = vvPuPurchaseRequisitionMapper.byDocumentnumberCode(s);
            if (vvPuPurchaseRequisition1 != null) {
                // 删除视图数据
                vvPuPurchaseRequisitionMapper.deletPurchaseRequisition(vvPuPurchaseRequisition1.getPurchaserequisitionid());
                vvPuPurchaseRequisitionBMapper.deletePurchaseRequisitionB(vvPuPurchaseRequisition1.getPurchaserequisitionid());
            }
            Header[] headers = ConnmeUtils.createHeaders();
            Map<String, Object> parameter = new HashMap<>();
            parameter.put("app_id", "64f00ef131adb70007ad6e6d");
            parameter.put("entry_id", "650ba4e4e5dec254a092b84c");
            List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameter, headers, s,"documentcode");
            if (jdyPurchaseOrders.size() == 0) {
               continue;
            }
            // 执行删除操作
            Map<String, Object> parameterDelete = new HashMap<>();
            parameterDelete.put("app_id", "64f00ef131adb70007ad6e6d");
            parameterDelete.put("entry_id", "650ba4e4e5dec254a092b84c");
            parameterDelete.put("data_id", jdyPurchaseOrders.get(0).get_id());
            String jsonSaleDelete = JSONUtil.toJsonStr(parameterDelete);
         ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/delete", headers, jsonSaleDelete);
        }
    }
}
